import React, { Component } from 'react';
import { Button, message } from 'antd';
import Info from 'components/info';
import { connect } from 'react-redux';
import { messages } from 'share/common';
import menuRoute from 'routes/menuRoute';
import { tableDecorator } from './decorator';
import SlideFrame from 'components/slide-frame';
import InvoiceCheckForm from './invoice-check-form';
import AdvancedTable from 'components/advanced-table';
import 'styles/request-and-expense-settings/invoice-check.scss';
import invoiceManagementService from './invoice-management-service';

const SimpleTable = AdvancedTable.SimpleTable;

@tableDecorator
class InvoiceToCost extends Component {
  state = {
    columns: ['priority','companyNameList', 'operation'],
    searchColumns: ['companyNameList'],
    invoiceManagementDetail: menuRoute.getRouteItem('invoice-management-control-detail', 'key'),
  };

  modeRef = null;

  handleRowClick = (record) => {
    const { receiptCheckOptId } = record;
    this.context.router.push(this.state.invoiceManagementDetail.url.replace(':id', receiptCheckOptId));
  };

  handleNew = () => {
    const { tenant } = this.props;
    SlideFrame.open({
      title: messages('common.create')/*新建*/,
      content: InvoiceCheckForm,
      afterClose: this.afterCloseSlideFrame,
      params: {
        tenant
      }
    }, this);
  };

  handleDelete = async (record) => {
    const { receiptCheckOptId } = record;
    this.setState({ loading: true });
    try {
      await invoiceManagementService.deleteInvoiceCheck(receiptCheckOptId);
      message.success(messages('common.delete.successful')/*删除成功*/);
      this.modeRef.getList();
    }catch (e) {

    }
    this.setState({ loading: false });
  };
  afterCloseSlideFrame = (flag) => {flag && this.modeRef.getList();};

  service = (page, pageSize, searchParams) => invoiceManagementService.getInvoiceToCost({
    page,
    pageSize,
    ...searchParams
  });

  render() {
    const { columns, searchColumns, loading } = this.state;
    const { dataSource, attributes } = this.handleInfo('invoice-to-cost');
    return (
      <section className="invoice-check">
        <SimpleTable
          onRef={ref => this.modeRef = ref}
          onRow={record => ({ onClick: () => {this.handleRowClick(record);} })}
          rowKey='receiptCheckOptId'
          loading={loading}
          columns={this.handleColumns(columns)}
          service={this.service}
          searchForm={this.handleSearchForm(searchColumns)}
        >
          <Info
            dataSource={dataSource}
            attributes={attributes}
          />
          <Button type="primary" onClick={this.handleNew}>
            {messages('common.create')/*新建*/}
          </Button>
        </SimpleTable>
      </section>
    );
  }
}

function mapStateToProps(state) {
  return {
    tenant: state.login.tenant,
  };
}


InvoiceToCost.contextTypes = {
  router: React.PropTypes.object
};

InvoiceToCost.propTypes = {};

export default connect(mapStateToProps)(InvoiceToCost);
